<?php 
$req_path="./";
include_once($req_path."include/ini.php");
require_once($req_path."include/db.php");

class M_login extends DB{
	private $dbConn;
	private $rowCount="0";
	private $sqlSelect ="select u.*, b.Type , b.BranchCode, g.Discount ,case when g.Type != 1 then 0 else 1 end  as isHo from tb_user u LEFT JOIN tb_branch b ON b.ID = u.BranchID left join tb_usergroup g on g.GroupID=u.GroupID  where u.IsActived=1";
	private $message="";
	private $result_val=array();
	
	function M_login(){
		parent::__construct();
	}

	function checkUserLogin($name, $password){
		$sqlscript= $this->sqlSelect;
		$sqlscript.= " and UserName='". $name."'";
		$sqlscript.= " and Password ='". $password."'";
		//print $sqlscript;
		$result = $this->ExecuteData($sqlscript);
		$rows =$result->RecordCount();

		//Set Row
		$this->rowCount=$rows;
		
		if ($rows > 0){
			//Set result
			$this->reslt_val=$result->FetchRow();
			return 1; // Completed
		}else{
			if ($this->checkUserNameExist($name)==1){
				return 2;
				//$this->$message = "Incorrect password.";
			}else {
				return 3;
				//$this->$message = "Incorrect username.";
			}
			//return 0;
		}

		///return $result;
	}
	function CheckUserAuthen($uid=0){
		$sql = "SELECT s.ScreenName,g.View, g.Add, g.Edit, g.Deleted
		From tb_user u
		left join tb_group_permission g on u.GroupID= g.GroupID
		left join tb_screen_list s on s.id=g.ScreenID
		where UserID=".$uid;
	
		$result = $this->ExecuteData($sql);
		$arr=array();
		while($row = $result->FetchRow()){
			$arr[$row["ScreenName"]] = $row;
		}
		
		return $arr;//$result->FetchRow();
	}
	
	function getErrorMessage(){
		return $this->$message;
	}
	function getUserDetail(){
		return $this->reslt_val;
	}
	function checkUserNameExist($name){
		$sqlscript= "select * from tb_user Where 1=1 ";
		$sqlscript.= " and UserName='". $name."'";
		
		$result = $this->ExecuteData($sqlscript);
		
		$rows =$result->RecordCount();
		
		return $rows>0? 1:0; // 1 exists , 0 not exists
	}
	 

	function getRowCount(){
		return $this->rowCount;
	}
   
	function getTotalPage(){
		return $this->totalpage;
	}
}
?>